package com.dd.ljzapplication.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dd.ljzapplication.R;

/**
 * 在自定义控件中设置字体大小要调用，setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
 */
public class SingleItem extends RelativeLayout {

	private Context mContext;
	private View mItem;
	private ImageView iv_icon, iv_arrow;
	private TextView tv_title, tv_label;
	private RelativeLayout rootLayout;

	public SingleItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		initView();

		TypedArray mArray = mContext.obtainStyledAttributes(attrs,
				R.styleable.SingleItem);
		String title = mArray.getString(R.styleable.SingleItem_android_text);
		setTitle(title);
		String lab = mArray.getString(R.styleable.SingleItem_SingleItemLabel);
		setLabel(lab);
//		int titleColor = mArray.getColor(R.styleable.SingleItem_android_textColor,
//				mContext.getResources().getColor(R.color.black));
//		setTitleColor(titleColor);
//
//		int labColor = mArray.getColor(R.styleable.SingleItem_labelColor, mContext.getResources().getColor(R.color.black));
//		setLabelColor(labColor);
//










//		float titleSize = mArray.getDimensionPixelOffset(R.styleable.SingleItem_android_textSize, 16);
//		setTitleSize(titleSize);

		float titleSize = mArray.getDimensionPixelSize(R.styleable.SingleItem_android_textSize, 15);
		setTitleSize(titleSize);
//
//		float titleSize = mArray.getDimensionPixelOffset(R.styleable.SingleItem_android_textSize, 16);
//		setTitleSize(titleSize);
//
//		float labSize = mArray.getDimension(R.styleable.SingleItem_labelSize, mContext.getResources().getDimension(R.dimen.big_text_size));
//		setLabelSize(labSize);


		float dimension = mArray.getDimension(R.styleable.SingleItem_titleSize, 14);
		setLabelSize(dimension);

		Drawable icon = mArray.getDrawable(R.styleable.SingleItem_android_src);
		iv_icon.setImageDrawable(icon);
//
//		int color = mArray.getColor(R.styleable.SingleItem_backgroundColor, Color.WHITE);
//		rootLayout.setBackgroundColor(color);
//		mArray.recycle();
	}


	private void initView() {
		mItem = LayoutInflater.from(mContext).inflate(R.layout.view_single_item, this, true);

		iv_icon = (ImageView) mItem.findViewById(R.id.item_icon);
		iv_arrow = (ImageView) mItem.findViewById(R.id.item_arrow);
		tv_title = (TextView) mItem.findViewById(R.id.item_title);
		tv_label = (TextView) mItem.findViewById(R.id.item_label);
		rootLayout = (RelativeLayout) mItem.findViewById(R.id.view_single_item_root);
	}

	public void setIcon(int resId) {
		iv_icon.setImageResource(resId);
		iv_icon.setVisibility(View.VISIBLE);
	}

	public void showArrow() {
		iv_arrow.setVisibility(View.VISIBLE);
	}


	public void hideArrow() {
		iv_arrow.setVisibility(View.INVISIBLE);
	}

	public void setTitle(int resId) {
		tv_title.setText(resId);
	}

	public void setTitle(CharSequence text) {
		tv_title.setText(text);
	}

	public void setTitleColor(int color) {
		tv_title.setTextColor(color);
	}

	public void setTitleSize(float size) {
		tv_title.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
	}

	public void setLabel(int resId) {
		if (resId != 0) {
			tv_label.setText(resId);
			tv_label.setVisibility(View.VISIBLE);
		} else
			tv_label.setVisibility(View.GONE);

	}

	public void setLabel(CharSequence text) {
		if (!TextUtils.isEmpty(text)) {
			tv_label.setText(text);
			tv_label.setVisibility(View.VISIBLE);
		} else
			tv_label.setVisibility(View.GONE);

	}

	public void setLabelColor(int color) {
		tv_label.setTextColor(color);
	}

	public void setLabelSize(float size) {
		tv_label.setTextSize(TypedValue.COMPLEX_UNIT_PX,size);
	}

	public void setLabelGravity(int gravity) {
		((LinearLayout) mItem.findViewById(R.id.label_layout)).setGravity(gravity);
	}


	public void setBackground(int resId) {
		rootLayout.setBackgroundResource(resId);
	}

	public void setBackgroundDrawable(Drawable background) {
		rootLayout.setBackgroundDrawable(background);
	}
}